package hw;

import jdk.jshell.execution.Util;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.util.*;
import java.util.List;


public class Doudizhu extends JFrame {

    private List<Card> pukes = new ArrayList<>();
    private List<Card> player1 = new ArrayList<>();
    private List<Card> player2 = new ArrayList<>();
    private List<Card> player3 = new ArrayList<>();
    private List<Card> base = new ArrayList<>();

    public Doudizhu() {
        this.setSize(950, 800);
        this.setTitle("我的窗体");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setLayout(null);

        this.init();
        this.xipai();
        this.fapai();

        //玩家一标签
        int x1 = 40;
        int y1 = 550;
        for (int i = 0; i < 17; i++) {
            Icon lableImg = hw.Util.getImageIcon("/images/" + (player1.get(i).getPointValue() + 3) + "_" + (player1.get(i).getTypeValue() + 1) + ".jpg");
            JLabel labelImg1 = new JLabel(lableImg);
            labelImg1.setBounds(x1, y1, 105, 150);
            y1 -= 30;
            this.add(labelImg1);
        }

        //玩家二标签
        int x2 = 650;
        int y2 = 550;
        for (int i = 16; i >=0; i--) {
            Icon lableImg = hw.Util.getImageIcon("/images/" + (player2.get(i).getPointValue() + 3) + "_" + (player2.get(i).getTypeValue() + 1) + ".jpg");
            JLabel labelImg1 = new JLabel(lableImg);
            labelImg1.setBounds(x2, y2, 105, 150);
            x2 -= 30;
            this.add(labelImg1);
        }

        //玩家三标签
        int x3 = 790;
        int y3 = 550;
        for (int i = 0; i < 17; i++) {
            Icon lableImg = hw.Util.getImageIcon("/images/" + (player3.get(i).getPointValue() + 3) + "_" + (player3.get(i).getTypeValue() + 1) + ".jpg");
            JLabel labelImg1 = new JLabel(lableImg);
            labelImg1.setBounds(x3, y3, 105, 150);
            y3 -= 30;
            this.add(labelImg1);
        }

        //底牌标签
        int x4 = 450;
        int y4 = 60;
        for (int i = 0; i < 3; i++) {
            Icon lableImg = hw.Util.getImageIcon("/images/" + (base.get(i).getPointValue() + 3) + "_" + (base.get(i).getTypeValue() + 1) + ".jpg");
            JLabel labelImg1 = new JLabel(lableImg);
            labelImg1.setBounds(x4, y4, 105, 150);
            x4 -= 30;
            this.add(labelImg1);
        }

        JButton btn1 = new JButton("发牌");
        btn1.setBounds(400, 325, 100, 100);
        btn1.setFont(new Font("黑体", Font.BOLD,16));
        btn1.setBackground(Color.GREEN);
        this.add(btn1);

        this.setVisible(true);
    }


    // 初始化54张扑克牌
    public void init() {
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                pukes.add(new Card(points[i], types[j], i, j));
            }
        }
        pukes.add(new Card("大王", "", 14, 0));
        pukes.add(new Card("小王", "", 13, 0));
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(pukes);
        System.out.println(pukes);
    }

    // 发牌
    public void fapai() {
        player1 = pukes.subList(0, 17);
        player2 = pukes.subList(17, 34);
        player3 = pukes.subList(34, 51);
        base = pukes.subList(51, 54);

        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);
        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("底牌：" + base);
    }

    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointValue() == o2.getPointValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o1.getPointValue() - o2.getPointValue();
                }
            }
        });
    }


    public static void main(String[] args) {
        Doudizhu doudizhu = new Doudizhu();
        doudizhu.init();
        doudizhu.xipai();
        doudizhu.fapai();
    }
}
